<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="https://www.thymeleaf.org">
<head>
    <title>商品管理</title>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" media="all" href="/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" media="all" href="/layui/css/layui.css">
    <script type="text/javascript" src="/js/jquery-3.6.0.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
</head>
<script>
    function updateValues(ID){
        $("#UpdateID").val(ID);
    }
    function deleteValues(ID){
        $("#DeleteID").val(ID);
    }
    function FindProductType() {
        var type = $("#productType").val();
        data = {
            type: type
        }
        $.ajax({
            url: "selectProductType",
            type: "Post",
            data: data,
            datatype: "html",
            success: function (data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
    }
    function nameGSearch() {
        var name = $("#nameSearch").val();
        data = {
            name: name
        }
        $.ajax({
            url: "selectGName",
            type: "Post",
            data: data,
            datatype: "html",
            success: function (data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
    }
    function AddProduct(){
        var name = $("#AddName").val();
        var price = $("#AddPrice").val();
        var unit = $("#AddUnit").val();
        var type = $("#AddType").val();
        data = {
            name: name,
            price: price,
            unit: unit,
            type: type,
        }
        $.ajax({
            url:"/product/insertProduct",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
        setTimeout(function () {
            location.reload();
        }, 3000);
    }
    function WatchGPhoto(ID) {
        data = {
            id: ID
        }
        $.ajax({
            url:"/product/watchGPhoto",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#KGP").replaceWith(data);
            }
        })
    }
    function UpdateProduct(){
        var id = $("#UpdateID").val();
        var price = $("#UpdatePrice").val();
        data = {
            id: id,
            price: price,
        }
        $.ajax({
            url:"product/updateProduct",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
        setTimeout(function () {
            location.reload();
        }, 3000);
    }
    function DeleteProduct(){
        var id = $("#DeleteID").val();
        data = {
            id: id
        }
        $.ajax({
            url:"product/deleteProduct",
            type: "Post",
            data:data,
            datatype: "html",
            success: function(data, status) {
                $("#roomInformation").replaceWith(data);
            }
        })
        setTimeout(function () {
            location.reload();
        }, 3000);
    }
</script>
<body>
<div class="container mt-3" id="roomInformation">
    <h2>商品管理界面</h2>
    <br>
    <ul class="nav nav-pills" role="tablist">
        <li class="navbar-form navbar-left" role="search">
            <a class="form-group">
                <input type="text" class="form-control" id="nameSearch" placeholder="搜索名称"/>
            </a>
        </li>
        <li><button type="submit" class="btn btn-default" onclick="nameGSearch()">搜索</button></li>

        <div class="producttype">
            <select id="productType" onchange="FindProductType()" type="button" class="btn btn-light btn-sm">
                <option>请选择商品类型</option>
                <option value="all">全部</option>
                <option value="饮料">饮料</option>
                <option value="零食">零食</option>
                <option value="小吃">小吃</option>
            </select>
        </div>

        <li class="pagination"><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#add">添加商品</button></li>

    </ul>
    <div class="tab-content">
        <div id="home" class="container tab-pane active"><br>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>商品号</th>
                    <th>商品名称</th>
                    <th>商品价格</th>
                    <th>类型</th>
                    <th>图片</th>
                    <th>修改商品价格</th>
                    <th>删除商品</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="product:${products}">
                    <td th:text="${product.id}"></td>
                    <td th:text="${product.name}"></td>
                    <td th:text="${product.price} + '/' + ${product.unit}"></td>
                    <td th:text="${product.type}"></td>
                    <td><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#photo" th:onclick="|javascript:WatchGPhoto('${product.id}')|">查看</button></td>
                    <td><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#modify" th:onclick="|javascript:updateValues('${product.id}')|">修改</button></td>
                    <td><button type="button" class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#fire" th:onclick="|javascript:deleteValues('${product.id}')|">删除</button></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal picture" id="photo">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:650px">

            <div class="modal-header">
                <h3 class="modal-title">商品图片</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal" onclick="location.reload()"></button>
            </div>

            <div class="modal-body" id="KGP">
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="location.reload()">关闭</button>
            </div>

        </div>
    </div>
</div>

<div class="modal alter" id="modify">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:650px">

            <div class="modal-header">
                <h4 class="modal-title">更新商品价格</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <table class="table table-bordered">
                    <thead>
                    </thead>
                    <tbody>
                    <tr>
                        <td style="font-size:22px">商品号</td><td><input id="UpdateID" name="UpdateID" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">更新价格</td><td><input id="UpdatePrice" name="UpdatePrice" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="UpdateProduct()">确定</button>
            </div>

        </div>
    </div>
</div>

<div class="modal Add" id="add">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <div class="modal-header">
                <h3 class="modal-title">添加商品</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <table class="table table-bordered">
                    <tbody>
                    <tr>
                        <td style="font-size:22px">商品名称</td><td><input id="AddName" name="AddName" style="font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">价格</td><td><input id="AddPrice" name="AddPrice" style="font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">单位</td>
                        <td>
                            <select id="AddUnit" name="AddUnit" style="font-size:22px" type="button" class="btn btn-light btn-sm">
                                <option value="瓶">瓶</option>
                                <option value="罐">罐</option>
                                <option value="盒">盒</option>
                                <option value="包">包</option>
                                <option value="份">份</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">类型</td>
                        <td>
                            <select id="AddType" name="AddType" style="font-size:22px" type="button" class="btn btn-light btn-sm">
                                <option value="饮料">饮料</option>
                                <option value="零食">零食</option>
                                <option value="小吃">小吃</option>
                            </select>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="AddProduct()">确定</button>
            </div>

        </div>
    </div>
</div>

<div class="modal Fire" id="fire">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:650px">

            <div class="modal-header">
                <h3 class="modal-title">删除商品</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <table class="table table-bordered">
                    <thead>
                    </thead>
                    <tbody>
                    <tr>
                        <td style="font-size:22px">商品号</td><td><input readonly="readonly" id="DeleteID" name="DeleteID" style="height:32px;width:300px;font-size:22px"></td>
                    </tr>
                    <tr>
                        <td style="font-size:22px">是否删除该商品？</td><td style="height:32px;width:300px;font-size:22px">永久删除该商品（真的很久）</td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="DeleteProduct()">是</button>
            </div>

        </div>
    </div>
</div>

</body>
</html>